/**
 * Factory creatures class
 * Uses to get data of Aliens and Humans from DB
 * Uses to place humans and aliens to map
 */
class xT_Factory_Humans extends Actor notplaceable;

//=============================================================================
// Variables
//=============================================================================
var private xT_GameInfo							mGameInfo;
var private xDB_Manager			                mDatabaseManager;
var private xDB_DLLAPI                          mDLLAPI;
var Rotator                                     SpawnRotation; //Rotation of creature.
var array<x_Data_Pawn>                          HumanData; //Pawn characteristics

var class<xT_Pawn>                              DefaultPawnClass;

//=============================================================================
// Events
//=============================================================================
event PostBeginPlay()
{
	Super.PostBeginPlay();	
	mGameInfo = xT_GameInfo(WorldInfo.Game);
	mDatabaseManager = mGameInfo.getDBMgr();
	mDLLAPI = mDatabaseManager.getDLLAPI();
}

//=============================================================================
// Functions: Add human units to map
//=============================================================================
function AddHumans()
{
	GetHumanData(); //get main data
	AddUnits();

	HumanData.Remove(0,HumanData.Length-1); // Clear HumanData array
}

/** Fill Data array from gameplay DB */
function GetHumanData()
{
	local x_Data_Pawn Data;

	mDLLAPI.SQL_selectDatabase(mDatabaseManager.mGameplayDatabaseIdx);

	if (mDLLAPI.SQL_queryDatabase("SELECT * FROM UNITS;"))
	{
		while(mDLLAPI.SQL_nextResult())
		{
			Data = new()class'x_Data_Pawn';
			Data.InitStrings();

			mDLLAPI.SQL_getIntVal("ID", Data.Invenotory_ID);	
			mDLLAPI.SQL_getIntVal("ID_ICON", Data.Icon_ID);
			mDLLAPI.SQL_getIntVal("ID_ARMOR", Data.Armor_ID);
			mDLLAPI.SQL_getIntVal("ID_RANK", Data.Rank_ID);
			mDLLAPI.SQL_getIntVal("ID_SEX", Data.Sex_ID);

			mDLLAPI.SQL_getStringVal("NAME", Data.DisplayName);
			mDLLAPI.SQL_getIntVal("EXPERIENCE", Data.Experience);
			mDLLAPI.SQL_getIntVal("LEVEL", Data.Level);
			mDLLAPI.SQL_getIntVal("VITALITY", Data.InitialVitality);
			mDLLAPI.SQL_getIntVal("ACCURACY", Data.InitialAccuracy);

			HumanData.AddItem(Data);
			Data = none;
		}
	}
	
}

/** Add soldiers to map */
private function AddUnits()
{
	local xT_Pawn lNewPawn;
	//local xT_Weapons WeaponTemplate; //????????
	local int il;
	local string lUnitClass;
	local array<xT_Pawn_Human>	AllUnits;
	local array<xT_Marker_SpawnPoint_Human> lSpawnPoints;
	local xT_Marker_SpawnPoint_Human lCurrentSpawnPoint;
	local Vector lSpawnLoc;
	local Rotator lSpawnRot;

	foreach WorldInfo.AllNavigationPoints(class'xT_Marker_SpawnPoint_Human', lCurrentSpawnPoint)
	{
		lSpawnPoints.AddItem(lCurrentSpawnPoint);
	}

	mDLLAPI.SQL_selectDatabase(mDatabaseManager.mContentDatabaseIdx);
	
	// Get rest of human data from content DB
	for(il=0; il<HumanData.Length; ++il)
	{
		if (mDLLAPI.SQL_queryDatabase("SELECT * FROM XCOM_ARMOR WHERE ID="$(HumanData[il].Armor_ID)$";"))
		{
			`xDB_InitString(lUnitClass,50);
			while(mDLLAPI.SQL_nextResult())
			{
				mDLLAPI.SQL_getStringVal("CLASS_REF", lUnitClass);
				lSpawnLoc = lSpawnPoints[il].Location;
				lSpawnRot = lSpawnPoints[il].Rotation;
				lSpawnPoints[il].Destroy();
				lNewPawn = Spawn(class'xT_Pawn_Human_Light', mGameInfo.GetPlayerController(), 'Human', lSpawnLoc, lSpawnRot, , True);
				if(lNewPawn != None)
				{
					mGameInfo.SetPlayerDefaults(lNewPawn);
					lNewPawn.SetCharacterClassFromInfo();
					lNewPawn.Data = HumanData[il];
					lNewPawn.Data.InitData();
					lNewPawn.UpdateParameters();
					
					////WeaponTemplate = spawn(class'xT_Weapons_AlienPlasma');
					//WeaponTemplate=spawn(class'xT_Weapons_Riffle');
					////WeaponTemplate=spawn(class'xcT_Weapons_Grenade');
					//lNewPawn.CreateInventoryFromName("Weapons_Riffle.Human.Riffle"); //give it weapon
					////lNewPawn.CreateInventoryFromTemplate(WeaponTemplate);  
					
					AllUnits.AddItem(xT_Pawn_Human(lNewPawn));
				}
				else `log("ERROR: Pawn Spawn Failure");
				lNewPawn = none;
			}	
			xT_PlayerController(mGameInfo.GetPlayerController()).AllUnits = AllUnits;
		}
	}
}

//=============================================================================
// Default Properties
//=============================================================================
DefaultProperties
{
	DefaultPawnClass = class'xT_Pawn_Human'
}